package com.newsystem.controller;


import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.newsystem.MyUtils.CommonResult;
import com.newsystem.mapper.MediumMapper;
import com.newsystem.mapper.UserMapper;
import com.newsystem.pojo.Medium;
import com.newsystem.pojo.NewsCover;
import com.newsystem.pojo.User;
import com.newsystem.pojo.vo.MediumInfo;
import com.newsystem.pojo.vo.MediumInfo2;
import com.newsystem.service.impl.MediumService;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * <p>
 * 媒体号 前端控制器
 * </p>
 *
 * @author chenlitao
 * @since 2022-11-06
 */
@RestController
@RequestMapping("/medium")
@RequiredArgsConstructor
public class MediumController {

    private final MediumService mediumService;
    private final MediumMapper mediumMapper;
    private final UserMapper userMapper;

    @ApiOperation("媒体号注册")
    @PutMapping("/register")
    public CommonResult<Object> register(User user, Medium medium){
        if(user.getAccount() == null){
            return CommonResult.failed("用户账号不能为空");
        }
        if(user.getPassword() == null){
            return CommonResult.failed("用户密码不能为空");
        }
        if(user.getRole()==2){
            return mediumService.insertOneMedium(user,medium);
        }
        else {
            return CommonResult.failed("请确定是否申请媒体号");
        }
    }

    //todo mediumId修改成了userId
    @ApiOperation("媒体号查找自己信息")
    @GetMapping("/getinfo")
    public CommonResult<Object> getInfo(String userId){
        return mediumService.selectMediumInfoByMediumId(userId);
    }

    @ApiOperation("媒体号修改自己信息")
    @PutMapping("/update")
    public CommonResult<Object> update(MediumInfo2 mediumInfo){
        if(mediumMapper.updateMedium(mediumInfo) > 0){
            return CommonResult.success("修改成功");
        }
        return CommonResult.failed("修改失败");
    }

    //todo
    @ApiOperation("获取我的新闻列表")
    @GetMapping("/mynews")
    public CommonResult<Object> getMyNews(String userId,Integer startPage,Integer sumPage){
        startPage = (startPage-1) * sumPage;
        List<NewsCover> newsCoverList = mediumMapper.selectNewsCoverListByUserId(userId, startPage, sumPage);
        if (newsCoverList == null){
            return CommonResult.failed("获取我的新闻列表失败");
        }
        return CommonResult.success(newsCoverList);
    }
}

